package com.example.datlichkham;

import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.method.LinkMovementMethod;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.database.DatabaseHelp;
import com.example.database.TaiKhoanTb;


public class MainActivity extends ActionBarActivity {
    TextView txtDKTD;
    Button btnDN;
    EditText  edtName,edtPass;
    String username,password;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        edtName = (EditText) findViewById(R.id.edtName);
        edtPass = (EditText) findViewById(R.id.edtPass);
        DatabaseHelp database = new DatabaseHelp(this);
        try {
            database.createDataBase();
        } catch (IOException e) {
            e.printStackTrace();
        }
        txtDKTD = (TextView) findViewById(R.id.txtDKTD);
        txtDKTD.setMovementMethod(LinkMovementMethod.getInstance());


        txtDKTD.setOnClickListener(new  View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, DangKy.class);
                startActivity(intent);

            }
        });

        btnDN = (Button) findViewById(R.id.btnDangnhap);
        btnDN.setOnClickListener(new  View.OnClickListener(){
            @Override
            public void onClick(View v) {

                dangnhap();
            }
        });
    }
    public void dangnhap() {
        if (doValidate() == true) {
            String username = edtName.getText().toString();
            String password = edtPass.getText().toString();

            TaiKhoanTb taiKhoan = new TaiKhoanTb(MainActivity.this);
            Cursor cursor = taiKhoan.getTaikhoan(username, password);
            if (cursor != null) {
            	
                Intent resultIntent = new Intent(MainActivity.this, DatLichKham.class);
                resultIntent.putExtra("TenDangNhap", username);
                resultIntent.putExtra("MatKhau", password);
                startActivity(resultIntent);
//                setResult(Activity.RESULT_OK, resultIntent);
                Toast.makeText(MainActivity.this, "Đăng nhập thành công !!",
                        Toast.LENGTH_SHORT).show();
                finish();
            } else {
                Toast.makeText(this, "Nhập lại thông tin", Toast.LENGTH_SHORT)
                        .show();
            }
        }
    }
    protected boolean doValidate() {
        String strUsername = edtName.getText().toString().trim();
        String strPassword = edtPass.getText().toString().trim();
        if (strUsername.equals("") || strUsername.length() == 0) {
            edtName.setError("Truong tên đăng nhập không được rỗng!");
            return false;
        }

        if (strPassword.equals("") || strPassword.length() == 0) {
            edtPass.setError("Trường mật khẩu không được rỗng!");
            return false;
        }

        return true;
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
